package com.yy.invoice.controller;

import com.yy.invoice.entity.Invoice;
import com.yy.invoice.entity.Result;
import com.yy.invoice.service.InvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("invoice")
@Slf4j
@Api(tags = "发票管理")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @ApiOperation("默认查询当前月份的发票")
    @GetMapping("/select")
    public Result select(HttpServletRequest request){
        return invoiceService.select(request);
    }

    /**
     * 以发票上传的时间来查询发票
     * @param month
     * @param request
     * @return
     */
    @ApiOperation("根据月份查询发票信息")
    @GetMapping("/select/month/{month}")
    public Result selectByMonth(@ApiParam("需要查询的月份") @PathVariable("month") String month, HttpServletRequest request){
        return invoiceService.selectByMonth(month,request);
    }

    @ApiOperation("根据月份统计发票的总额")
    @GetMapping("/sum/month/{month}")
    public Result sumByMonth(@ApiParam("需要统计的月份") @PathVariable("month") String month,HttpServletRequest request){
        return invoiceService.sumByMonth(month,request);
    }

    @ApiOperation("删除发票")
    @DeleteMapping("/delete")
    public Result deleteInvoice(@ApiParam(value = "需要删除的所有发票",required = true) @RequestBody List<Invoice> invoices,HttpServletRequest request){
        return invoiceService.delete(invoices,request);
    }



    @ApiOperation("避免发票识别出错，添加手动更新发票")
    @PostMapping("/updateInvoice")
    public Result updateInvoice(
            @RequestParam("invoiceCode")  String invoiceCode,
            @RequestParam("invoiceNum") String invoiceNum,
            @RequestParam("validate") String validate,
            @RequestParam("totalAmount") String totalAmount,
            @RequestParam("invoiceId") Integer invoiceId
    ){
        return invoiceService.updateInvoice(invoiceCode,invoiceNum,validate,totalAmount,invoiceId);
    }
}
